import Vue from 'vue'
import Router from 'vue-router'
const Login = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ './components/Login.vue')
const Home = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ './components/Home.vue')
const Welcome = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ './components/Welcome.vue')
const Users = () =>
    import ( /* webpackChunkName: "users_right_Roles" */ './components/user/Users.vue')
const Rights = () =>
    import ( /* webpackChunkName: "users_right_Roles" */ './components/power/rights.vue')
const Roles = () =>
    import ( /* webpackChunkName: "users_right_Roles" */ './components/power/roles.vue')
const Cate = () =>
    import ( /* webpackChunkName: "cate_params" */ './components/goods/Cate.vue')
const Params = () =>
    import ( /* webpackChunkName: "cate_params" */ './components/goods/params.vue')
const List = () =>
    import ( /* webpackChunkName: "list_add" */ './components/goods/List.vue')
const Add = () =>
    import ( /* webpackChunkName: "list_add" */ './components/goods/Add.vue')
const Order = () =>
    import ( /* webpackChunkName: "order_report" */ './components/order/Order.vue')
const Report = () =>
    import ( /* webpackChunkName: "order_report" */ './components/report/Report.vue')

Vue.use(Router)

const router = new Router({
        routes: [
            { path: '/login', component: Login },
            { path: '/', redirect: '/login' },
            {
                path: '/home',
                component: Home,
                redirect: '/welcome',
                children: [
                    { path: '/welcome', component: Welcome },
                    { path: '/users', component: Users },
                    { path: '/rights', component: Rights },
                    { path: '/roles', component: Roles },
                    { path: '/categories', component: Cate },
                    { path: '/params', component: Params },
                    { path: '/goods', component: List },
                    { path: '/goods/add', component: Add },
                    { path: '/orders', component: Order },
                    { path: '/reports', component: Report }
                ]
            }

        ]
    })
    // 路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
export default router